﻿using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using YAPACS.Workflows;

namespace YAPACS.RisServer.Workflows.Report;

public class FollowUpRecord(IRepository<BusinessObjects.Exams.Report, Guid> reportRepository) : WorkflowBase, IFollowUpRecord, ITransientDependency
{
    private readonly IRepository<BusinessObjects.Exams.Report, Guid> _reportRepository = reportRepository;

    protected override async Task<WorkflowOutput> InvokeCoreAsync(WorkflowInput input)
    {
        const string key = "Input";
        if (input.ContainsKey(key) && input.GetValueOrDefault(key) is Dictionary<string, object> data)
        {
            if (Guid.TryParse(data.GetValueOrDefault("reportId")?.ToString(), out Guid reportId))
            {
                var report = await _reportRepository.GetAsync(reportId);

                var schema = data.GetValueOrDefault("schema")?.ToString();
                if (!string.IsNullOrWhiteSpace(schema))
                {
                    report.FollowupRecordSchema = schema;
                }
                data.Remove("schema");
                report.FollowupRecord = data["followupRecord"].Serialize();
                report.FollowupResult = data["followupResult"]?.ToString();
                //处理逻辑
                return new WorkflowOutput
                {
                    Success = true,
                    Data = data
                };
            }
        }
        return new WorkflowOutput
        {
            Success = false,
            Error = "未找到Exam对象",
        };
    }
}
